package cn.gcyl.module.biz.controller.admin.platformregistrationinformation;

import cn.gcyl.module.biz.dal.dataobject.SysMemberUserDO;
import cn.gcyl.module.biz.dal.dataobject.platforminformation.PlatformInformationDO;
import cn.gcyl.module.biz.dal.dataobject.platformregistrationinformation.PlatformUserRegistrationInformationPO;
import cn.gcyl.module.biz.dal.mysql.SysMemberUserMapper;
import cn.gcyl.module.biz.dal.mysql.platforminformation.PlatformInformationMapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.gcyl.framework.common.pojo.PageParam;
import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.pojo.CommonResult;
import cn.gcyl.framework.common.util.object.BeanUtils;
import static cn.gcyl.framework.common.pojo.CommonResult.success;

import cn.gcyl.framework.excel.core.util.ExcelUtils;

import cn.gcyl.framework.apilog.core.annotation.ApiAccessLog;
import static cn.gcyl.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.gcyl.module.biz.controller.admin.platformregistrationinformation.vo.*;
import cn.gcyl.module.biz.dal.dataobject.platformregistrationinformation.PlatformRegistrationInformationDO;
import cn.gcyl.module.biz.service.platformregistrationinformation.PlatformRegistrationInformationService;

@Tag(name = "管理后台 - 平台报名信息")
@RestController
@RequestMapping("/biz/platform-registration-information")
@Validated
public class PlatformRegistrationInformationController {

    @Resource
    private PlatformRegistrationInformationService platformRegistrationInformationService;
    @Resource
    private PlatformInformationMapper platformInformationMapper;
    @Resource
    private SysMemberUserMapper sysMemberUserMapper;

    @PostMapping("/create")
    @Operation(summary = "创建平台报名信息")
//    @PreAuthorize("@ss.hasPermission('biz:platform-registration-information:create')")
    public CommonResult<Long> createPlatformRegistrationInformation(@Valid @RequestBody PlatformRegistrationInformationSaveReqVO createReqVO) {
        return success(platformRegistrationInformationService.createPlatformRegistrationInformation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新平台报名信息")
//    @PreAuthorize("@ss.hasPermission('biz:platform-registration-information:update')")
    public CommonResult<Boolean> updatePlatformRegistrationInformation(@Valid @RequestBody PlatformRegistrationInformationSaveReqVO updateReqVO) {
        platformRegistrationInformationService.updatePlatformRegistrationInformation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除平台报名信息")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('biz:platform-registration-information:delete')")
    public CommonResult<Boolean> deletePlatformRegistrationInformation(@RequestParam("id") Long id) {
        platformRegistrationInformationService.deletePlatformRegistrationInformation(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除平台报名信息")
//                @PreAuthorize("@ss.hasPermission('biz:platform-registration-information:delete')")
    public CommonResult<Boolean> deletePlatformRegistrationInformationList(@RequestParam("ids") List<Long> ids) {
        platformRegistrationInformationService.deletePlatformRegistrationInformationListByIds(ids);
        return success(true);
    }

//    @GetMapping("/get")
//    @Operation(summary = "获得平台报名信息")
//    @Parameter(name = "id", description = "编号", required = true, example = "1024")
////    @PreAuthorize("@ss.hasPermission('biz:platform-registration-information:query')")
//    public CommonResult<PlatformRegistrationInformationRespVO> getPlatformRegistrationInformation(@RequestParam("id") Long id) {
//        PlatformRegistrationInformationDO platformRegistrationInformation = platformRegistrationInformationService.getPlatformRegistrationInformation(id);
//        return success(BeanUtils.toBean(platformRegistrationInformation, PlatformRegistrationInformationRespVO.class));
//    }
    @GetMapping("/get")
    @Operation(summary = "获得平台报名信息详情")
    public CommonResult<PlatformUserRegistrationInformationPO> getPlatformRegistrationInformationOne(
            @Valid PlatformUserRegistrationInformationPageReqVO pageReqVO) {
        PlatformUserRegistrationInformationPO platformRegistrationInformationOne = platformRegistrationInformationService.getPlatformRegistrationInformationOne(pageReqVO);
        return success(platformRegistrationInformationOne);
    }

    @GetMapping("/page")
    @Operation(summary = "获得平台报名信息分页")
//    @PreAuthorize("@ss.hasPermission('biz:platform-registration-information:query')")
    public CommonResult<PageResult<PlatformUserRegistrationInformationPO>> getPlatformRegistrationInformationPage(
            @Valid PlatformUserRegistrationInformationPageReqVO pageReqVO) {
        PageResult<PlatformUserRegistrationInformationPO> userPlatformRegistrationInformationPage = platformRegistrationInformationService
                .getUserPlatformRegistrationInformationPage(pageReqVO);
        return success(userPlatformRegistrationInformationPage);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出平台报名信息 Excel")
//    @PreAuthorize("@ss.hasPermission('biz:platform-registration-information:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPlatformRegistrationInformationExcel(@Valid PlatformUserRegistrationInformationPageReqVO pageReqVO,
                                                           HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PlatformUserRegistrationInformationPO> list = platformRegistrationInformationService
                .getUserPlatformRegistrationInformationPage(pageReqVO).getList();
        for (PlatformUserRegistrationInformationPO po : list) {
            if (po.getPlatformInformationId() != null) {
                PlatformInformationDO platformInformationDO = platformInformationMapper.selectById(po.getPlatformInformationId());
                if (platformInformationDO != null) {
                    po.setTitle(platformInformationDO.getTitle());
                    po.setTitleType(platformInformationDO.getTitleType());
                }
            }
            if (po.getUserId() != null) {
                SysMemberUserDO sysMemberUserDO = sysMemberUserMapper.selectById(po.getUserId());
                if (sysMemberUserDO != null) {
                    po.setNickname(sysMemberUserDO.getNickname());
                    po.setMobile(sysMemberUserDO.getMobile());
                }
            }
        }
        ExcelUtils.write(response, "平台报名信息.xls", "数据", PlatformUserRegistrationInformationPO.class,
                list);
    }
}